package com.ruoyi.web.controller.community;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.*;
import com.ruoyi.community.service.hospital.UserService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/community/help")
public class HelpVoteController extends BaseController {
    @Autowired
    HelpVoteMapper helpVoteMapper;

    @Autowired
    HelpVoteOptionMapper helpVoteOptionMapper;

    @Autowired
    HelpVoteResultMapper helpVoteResultMapper;

    @Autowired
    HelpVoteComment1Mapper helpVoteComment1Mapper;

    @Autowired
    HelpVoteComment2Mapper helpVoteComment2Mapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    HelpVoteSectionsMapper helpVoteSectionsMapper;

    @Autowired
    ISysUserService userService;

    @GetMapping("/sections")
    public List<HelpVoteSections> getSections(){
        return helpVoteSectionsMapper.selectAllSections();
    }

    @GetMapping("/votes")
    public TableDataInfo getHelpVotes(){
        startPage();
        System.out.println("aaaa");
        List<HelpVote> list = helpVoteMapper.selectHelpVotesByCommunityId(SecurityUtils.getCommunityId());
        return getDataTable(list);
    }

    @GetMapping("/vote/{voteId}")
    public HelpVoteForReturn getHelpVoteById(@PathVariable("voteId") Integer voteId){
        HelpVote helpVote = helpVoteMapper.selectByPrimaryKey(voteId);
        List<HelpVoteOption> helpVoteOptions = helpVoteOptionMapper.selectByVoteId(voteId);
        HelpVoteForReturn helpVoteForReturn = new HelpVoteForReturn();
        helpVoteForReturn.setHelpVote(helpVote);
        helpVoteForReturn.setHelpVoteOptions(helpVoteOptions);
        return helpVoteForReturn;
    }

    @DeleteMapping("/vote/{voteId}")
    public AjaxResult deleteHelpVoteById(@PathVariable("voteId") Integer voteId){
        return toAjax(helpVoteMapper.deleteByPrimaryKey(voteId));
    }

    @PutMapping("/vote")
    public AjaxResult updateVoteContent(@RequestBody HelpVote helpVote) {
        return toAjax(helpVoteMapper.updateByPrimaryKey(helpVote));
    }

//    @PutMapping("/voteOption")
//    public AjaxResult updateVoteContent(@RequestBody HelpVote helpVote) {
//        return toAjax(helpVoteMapper.updateByPrimaryKey(helpVote));
//    }

    @PostMapping("/vote")
    public AjaxResult addVote(@RequestBody HelpVoteForReturn helpVoteForReturn) {
        HelpVote helpVote = helpVoteForReturn.getHelpVote();
        helpVote.setCommunityId(SecurityUtils.getCommunityId());
        helpVote.setUserId(SecurityUtils.getUserID());
        int r1 = helpVoteMapper.insert(helpVote);
        int voteID = helpVote.getVoteId();
        List<HelpVoteOption> helpVoteOptionList = helpVoteForReturn.getHelpVoteOptions();
        int r2 = 1;
        for (int i = 0; i < helpVoteOptionList.size(); i++) {
            HelpVoteOption helpVoteOption = helpVoteOptionList.get(i);
            helpVoteOption.setVoteId(voteID);
            int temp = helpVoteOptionMapper.insert(helpVoteOption);
            if (temp == 0) {
                r2 = 0;
            }
        }
        AjaxResult ajaxResult;
        if (r1 > 0 && r2 > 0) {
            ajaxResult = AjaxResult.success();
        }
        else {
            ajaxResult = AjaxResult.error();
        }
        return ajaxResult;
    }

    @GetMapping("/voteResults/{voteId}")
    public List<HelpVoteResultForReturn> getVoteResultsById(@PathVariable("voteId") Integer voteId){
        List<HelpVoteResult> helpVoteResultList = helpVoteResultMapper.selectByVoteId(voteId);
        List<HelpVoteResultForReturn> result = new ArrayList<>();
        for (int i = 0; i < helpVoteResultList.size(); i++) {
            HelpVoteResultForReturn temp = new HelpVoteResultForReturn();
            temp.setDate(helpVoteResultList.get(i).getDate());
            temp.setResultId(helpVoteResultList.get(i).getResultId());
            temp.setVoteId(helpVoteResultList.get(i).getVoteId());
            SysUser sysUser = sysUserMapper.selectUserById(helpVoteResultList.get(i).getUserId(),SecurityUtils.getCommunityId());
            temp.setUserId(sysUser.getUserId());
            temp.setUserName(sysUser.getUserName());
            HelpVoteOption helpVoteOption = helpVoteOptionMapper.selectByPrimaryKey(helpVoteResultList.get(i).getOptionId());
            temp.setOption(helpVoteOption);
            result.add(temp);
        }
        return result;
    }

    @GetMapping("/voteComments/{voteId}")
    public List<HelpVoteComments> getVoteCommentsById(@PathVariable("voteId") Integer voteId){
        List<HelpVoteComments> helpVoteCommentsList = new ArrayList<>();
        List<HelpVoteComment1> helpVoteComment1List = helpVoteComment1Mapper.selectCommentsByVoteId(voteId);
        for (int i = 0; i < helpVoteComment1List.size(); i++) {
            HelpVoteComments helpVoteComment = new HelpVoteComments();
            Long userId = helpVoteComment1List.get(i).getUserId();
            SysUser u = userService.selectUserById(userId);
            helpVoteComment1List.get(i).setUserName(u.getUserName());
            helpVoteComment1List.get(i).setUserPicture(u.getAvatar());
            helpVoteComment.setHelpVoteComment1(helpVoteComment1List.get(i));
            int comment1Id = helpVoteComment1List.get(i).getCommentId();
            List<HelpVoteComment2> helpVoteComment2List = helpVoteComment2Mapper.selectCommentsByComment1Id(comment1Id);
            for (int j = 0; j < helpVoteComment2List.size(); j++){
                HelpVoteComment2 h2 = helpVoteComment2List.get(j);
                Long userId2 = h2.getUserId();
                SysUser u2 = userService.selectUserById(userId2);
                h2.setUserName(u2.getUserName());
                h2.setUserPicture(u2.getAvatar());
            }
            helpVoteComment.setHelpVoteComment2List(helpVoteComment2List);
            helpVoteCommentsList.add(helpVoteComment);
        }
        return helpVoteCommentsList;
    }

    @PostMapping("/voteComment1")
    public AjaxResult addVoteComment1(@RequestBody HelpVoteComment1 voteComment1) {
        voteComment1.setUserId(SecurityUtils.getUserID());
        voteComment1.setLike(0);
        voteComment1.setDate(new Date());
        return toAjax(helpVoteComment1Mapper.insert(voteComment1));
    }

    @PostMapping("/voteComment2")
    public AjaxResult addVoteComment2(@RequestBody HelpVoteComment2 voteComment2) {
        voteComment2.setUserId(SecurityUtils.getUserID());
        voteComment2.setLike(0);
        voteComment2.setDate(new Date());
        return toAjax(helpVoteComment2Mapper.insert(voteComment2));
    }

    @DeleteMapping("/voteComment1/{commentId}")
    public AjaxResult deleteComment1ById(@PathVariable("commentId") Integer commentId){
        return toAjax(helpVoteComment1Mapper.deleteByPrimaryKey(commentId));
    }

    @DeleteMapping("/voteComment2/{commentId}")
    public AjaxResult deleteComment2ById(@PathVariable("commentId") Integer commentId){
        return toAjax(helpVoteComment2Mapper.deleteByPrimaryKey(commentId));
    }
}
